from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from unittest import TestCase


class TestOnlineLoan(TestCase):

    # 正常申请贷款用例
    def testOnlineLoan1(self):
        expect = "24条"
        username = "lisi"
        password = "123456"
        pwd = '666666'

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")
        driver.maximize_window()

        ele1 = driver.find_element(By.XPATH, "//*[@id='username']")
        ele1.send_keys(username)

        # 输入密码
        ele2 = driver.find_element(By.ID, 'password')
        ele2.send_keys(password)

        # 点击登陆
        ele3 = driver.find_element(By.ID, 'login_btn')
        ele3.click()

        time.sleep(3)
        # 点击金融工具
        driver.find_element_by_xpath("//*[@id='leftbaraside']/div[2]/nav/ul/li[3]/a").click()
        time.sleep(3)

        # 点击安全网贷
        driver.find_element_by_link_text("安全网贷").click()
        time.sleep(3)

        # .输入借贷金额：10000
        driver.find_element_by_id("amount").send_keys('1000')
        time.sleep(2)

        # 输入借贷期限：10
        driver.find_element_by_id("term").send_keys('10')
        time.sleep(2)

        # 点击申请
        driver.find_element_by_id("submit").click()
        time.sleep(2)

        # 点击确认
        driver.find_element_by_xpath("//button[text()='确认']").click()
        time.sleep(1)

        driver.find_element_by_xpath("/html/body/div[2]/div/aside/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)

        driver.find_element_by_link_text("我的借贷").click()
        time.sleep(1)

        # 获取借款信息
        result = driver.find_element_by_xpath("/html/body/div/div/main/div/div/div/div/div[2]/div[2]/div["
                                              "2]/div/button[6]/span").text
        time.sleep(3)
        if expect != result:
            print("不通过!")
            driver.save_screenshot(
                r"C:\Users\Administrator\PycharmProjects\pythonProject\TEST\测试截图\申请网贷\lisi_1万_10.png")
        else:
            print("用例通过！")
        driver.quit()
        self.assertEqual(result, expect)

    # 确认申请贷款是否到账用例
    def testOnlineLoan2(self):
        expect = "账户余额：20000.00元"
        username = "lisi"
        password = "123456"
        pwd = '666666'

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")
        driver.maximize_window()

        ele1 = driver.find_element(By.XPATH, "//*[@id='username']")
        ele1.send_keys(username)

        # 输入密码
        ele2 = driver.find_element(By.ID, 'password')
        ele2.send_keys(password)

        # 点击登陆
        ele3 = driver.find_element(By.ID, 'login_btn')
        ele3.click()

        time.sleep(3)
        # 点击金融工具
        driver.find_element_by_xpath("//*[@id='leftbaraside']/div[2]/nav/ul/li[3]/a").click()
        time.sleep(3)

        # 点击安全网贷
        driver.find_element_by_link_text("安全网贷").click()
        time.sleep(3)

        # .输入借贷金额：10000
        driver.find_element_by_id("amount").send_keys('1000')
        time.sleep(2)

        # 输入借贷期限：10
        driver.find_element_by_id("term").send_keys('10')
        time.sleep(2)

        # 点击申请
        driver.find_element_by_id("submit").click()
        time.sleep(2)

        # 点击确认
        driver.find_element_by_xpath("//button[text()='确认']").click()
        time.sleep(1)

        driver.find_element_by_xpath("/html/body/div[2]/div/aside/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)

        driver.find_element_by_xpath("/html/body/div[2]/div/aside/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
        time.sleep(1)

        # 获取银行卡余额信息
        result = driver.find_element_by_xpath("/html/body/div/div/main/div/div/div[2]/div/div/div[2]/h4[2]").text
        time.sleep(3)
        if expect != result:
            print("不通过!")
            driver.save_screenshot(r"C:\Users\Administrator\PycharmProjects\pythonProject\TEST\测试截图\申请网贷\lisi_1万_10_2"
                                   r"万.png")
        else:
            print("用例通过！")
        driver.quit()
        self.assertEqual(result, expect)

    # 无卡申请贷款用例
    def testOnlineLoan3(self):
        expect = "申请失败,请先绑定个人信息"
        username = "lisi5"
        password = "123456"
        pwd = '666666'

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")
        driver.maximize_window()

        ele1 = driver.find_element(By.XPATH, "//*[@id='username']")
        ele1.send_keys(username)

        # 输入密码
        ele2 = driver.find_element(By.ID, 'password')
        ele2.send_keys(password)

        # 点击登陆
        ele3 = driver.find_element(By.ID, 'login_btn')
        ele3.click()

        time.sleep(3)
        # 点击金融工具
        driver.find_element_by_xpath("//*[@id='leftbaraside']/div[2]/nav/ul/li[3]/a").click()
        time.sleep(3)

        # 点击安全网贷
        driver.find_element_by_link_text("安全网贷").click()
        time.sleep(3)

        # .输入借贷金额：10000
        driver.find_element_by_id("amount").send_keys('1000')
        time.sleep(2)

        # 输入借贷期限：10
        driver.find_element_by_id("term").send_keys('10')
        time.sleep(2)

        # 点击申请
        driver.find_element_by_id("submit").click()
        time.sleep(2)

        # 点击确认
        driver.find_element_by_xpath("//button[text()='确认']").click()
        time.sleep(1)

        # 获取弹窗信息
        result = driver.find_element_by_xpath(
            "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div").text
        time.sleep(3)
        if expect != result:
            print("不通过!")
            driver.save_screenshot(
                r"C:\Users\Administrator\PycharmProjects\pythonProject\TEST\测试截图\申请网贷\lisi5_1万_10_3.png")
        else:
            print("用例通过！")
        driver.quit()
        self.assertEqual(result, expect)

    # 大额申请贷款用例
    def testOnlineLoan4(self):
        expect = "申请失败，申请额度必须是100的整数倍"
        username = "lisi"
        password = "123456"
        pwd = '666666'

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")
        driver.maximize_window()

        ele1 = driver.find_element(By.XPATH, "//*[@id='username']")
        ele1.send_keys(username)

        # 输入密码
        ele2 = driver.find_element(By.ID, 'password')
        ele2.send_keys(password)

        # 点击登陆
        ele3 = driver.find_element(By.ID, 'login_btn')
        ele3.click()

        time.sleep(3)
        # 点击金融工具
        driver.find_element_by_xpath("//*[@id='leftbaraside']/div[2]/nav/ul/li[3]/a").click()
        time.sleep(3)

        # 点击安全网贷
        driver.find_element_by_link_text("安全网贷").click()
        time.sleep(3)

        # .输入借贷金额：10
        driver.find_element_by_id("amount").send_keys('10')
        time.sleep(2)

        # 输入借贷期限：10
        driver.find_element_by_id("term").send_keys('10')
        time.sleep(2)

        # 点击申请
        driver.find_element_by_id("submit").click()
        time.sleep(2)

        # 点击确认
        driver.find_element_by_xpath("//button[text()='确认']").click()
        time.sleep(1)

        # 获取弹窗信息
        result = driver.find_element_by_xpath(
            "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div").text
        time.sleep(3)
        if expect != result:
            print("不通过!")
            driver.save_screenshot(
                r"C:\Users\Administrator\PycharmProjects\pythonProject\TEST\测试截图\申请网贷\lisi_1万_10_4.png")
        else:
            print("用例通过！")
        driver.quit()
        self.assertEqual(result, expect)

    # 小额拒绝申请贷款用例
    def testOnlineLoan5(self):
        expect = "申请失败，申请额度必须是100的整数倍"
        username = "lisi"
        password = "123456"
        pwd = '666666'

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")
        driver.maximize_window()

        ele1 = driver.find_element(By.XPATH, "//*[@id='username']")
        ele1.send_keys(username)

        # 输入密码
        ele2 = driver.find_element(By.ID, 'password')
        ele2.send_keys(password)

        # 点击登陆
        ele3 = driver.find_element(By.ID, 'login_btn')
        ele3.click()

        time.sleep(3)
        # 点击金融工具
        driver.find_element_by_xpath("//*[@id='leftbaraside']/div[2]/nav/ul/li[3]/a").click()
        time.sleep(3)

        # 点击安全网贷
        driver.find_element_by_link_text("安全网贷").click()
        time.sleep(3)

        # .输入借贷金额：10
        driver.find_element_by_id("amount").send_keys('10')
        time.sleep(2)

        # 输入借贷期限：10
        driver.find_element_by_id("term").send_keys('10')
        time.sleep(2)

        # 点击申请
        driver.find_element_by_id("submit").click()
        time.sleep(2)

        # 点击确认
        driver.find_element_by_xpath("//button[text()='确认']").click()
        time.sleep(1)

        # 获取弹窗信息
        result = driver.find_element_by_xpath(
            "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div").text
        time.sleep(3)
        if expect != result:
            print("不通过!")
            driver.save_screenshot(
                r"C:\Users\Administrator\PycharmProjects\pythonProject\TEST\测试截图\申请网贷\lisi_1万_10_5.png")
        else:
            print("用例通过！")
        driver.quit()
        self.assertEqual(result, expect)

    # 超长期限倍申请贷款用例
    def testOnlineLoan6(self):
        expect = "输入期限不合法！"
        username = "lisi"
        password = "123456"
        pwd = '666666'

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")
        driver.maximize_window()

        ele1 = driver.find_element(By.XPATH, "//*[@id='username']")
        ele1.send_keys(username)

        # 输入密码
        ele2 = driver.find_element(By.ID, 'password')
        ele2.send_keys(password)

        # 点击登陆
        ele3 = driver.find_element(By.ID, 'login_btn')
        ele3.click()

        time.sleep(3)
        # 点击金融工具
        driver.find_element_by_xpath("//*[@id='leftbaraside']/div[2]/nav/ul/li[3]/a").click()
        time.sleep(3)

        # 点击安全网贷
        driver.find_element_by_link_text("安全网贷").click()
        time.sleep(3)

        # .输入借贷金额：10
        driver.find_element_by_id("amount").send_keys('10')
        time.sleep(2)

        # 输入借贷期限：10000
        driver.find_element_by_id("term").send_keys('10000')
        time.sleep(2)

        # 点击申请
        driver.find_element_by_id("submit").click()
        time.sleep(2)

        # 点击确认
        driver.find_element_by_xpath("//button[text()='确认']").click()
        time.sleep(1)

        # 获取弹窗信息
        result = driver.find_element_by_xpath(
            "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div").text
        time.sleep(3)
        if expect != result:
            print("不通过!")
            driver.save_screenshot(
                r"C:\Users\Administrator\PycharmProjects\pythonProject\TEST\测试截图\申请网贷\lisi_1万_10_6.png")
        else:
            print("用例通过！")
        driver.quit()
        self.assertEqual(result, expect)

    # 超期申请贷款用例
    def testOnlineLoan7(self):
        expect = "申请成功！请等待管理员审核!"
        username = "lisi"
        password = "123456"
        pwd = '666666'

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")
        driver.maximize_window()

        ele1 = driver.find_element(By.XPATH, "//*[@id='username']")
        ele1.send_keys(username)

        # 输入密码
        ele2 = driver.find_element(By.ID, 'password')
        ele2.send_keys(password)

        # 点击登陆
        ele3 = driver.find_element(By.ID, 'login_btn')
        ele3.click()

        time.sleep(3)
        # 点击金融工具
        driver.find_element_by_xpath("//*[@id='leftbaraside']/div[2]/nav/ul/li[3]/a").click()
        time.sleep(3)

        # 点击安全网贷
        driver.find_element_by_link_text("安全网贷").click()
        time.sleep(3)

        # .输入借贷金额：10
        driver.find_element_by_id("amount").send_keys('10')
        time.sleep(2)

        # 输入借贷期限：9999
        driver.find_element_by_id("term").send_keys('9999')
        time.sleep(2)

        # 点击申请
        driver.find_element_by_id("submit").click()
        time.sleep(2)

        # 点击确认
        driver.find_element_by_xpath("//button[text()='确认']").click()
        time.sleep(1)

        # 获取借款信息
        result = driver.find_element_by_xpath("/html/body/div/div/main/div/div/div/div/div[2]/div[2]/div["
                                              "2]/div/button[6]/span").text
        time.sleep(3)
        if expect != result:
            print("不通过!")
            driver.save_screenshot(
                r"C:\Users\Administrator\PycharmProjects\pythonProject\TEST\测试截图\申请网贷\lisi_1万_10_7.png")
        else:
            print("用例通过！")
        driver.quit()
        self.assertEqual(result, expect)
